<?php
/**
 *
 */
namespace app\home\controller\goods;

use app\home\controller\Base;

class Api extends Base
{

	/**
	 * 商品列表接口
	 * @return [type] [description]
	 */
	public function goods(){
		$cate_id = !empty($this->request->param('cate_id'))?$this->request->param('cate_id'):'';
		$coin_id = !empty($this->request->param('coin_id'))?$this->request->param('coin_id'):'';
        $desc_name = !empty($this->request->param('desc_name'))?$this->request->param('desc_name'):'';
        $page = !empty($this->request->param('page'))?$this->request->param('page'):1;
        $orderBy = self::setOrderby($desc_name);
		$data=[];
        $perpagenum = 10;
		if(!empty($coin_id))$data=['coin_id'=>$coin_id];
		if(!empty($cate_id))$data=['cate_id'=>$cate_id];
		$rows = model('MallGoods')->getPageDatas($data,$page,$perpagenum,$orderBy);
		return json($rows);
	}

    /**
     * [goods 商品详情]
     * @return [type]      [description]
     */
	public function goodsDetails(){
		$params = $this->request->param();
        $id = isset($params['id'])?intval($params['id']):0;
        if(!$id)
            return json(['code'=>0,'msg'=>'参数错误']);;
        $data = model('MallGoods')->getOneDataById($id);
        //加入点击量
        model('MallGoods')->incNum(['id'=>$params['id']],'click_num',1);
		return json($data);
	}
    /**
     * [coinType 货币类型]
     * @return [type] [description]
     */
	public function coinType(){
        $data = model('Coin')->coinData();
		return json($data);
	}
    /**
     * [coinType 商品类型]
     * @return [type] [description]
     */
    public function cateType(){
        $data = model('MallCategory')->getPageDatas();
        return json($data);
    }

    private function setOrderby($desc_name){
        switch ($desc_name) {
            case 'rq':
                $order_by='click_num desc';
                break;
            case 'xl':
                $order_by='sales_num desc';
                break;
            case 'jg':
                $order_by='total desc';
                break;
            default:
                $order_by='sales_num desc,id desc';
                break;
        }
        return $order_by;
    }
}